﻿$(function () {
	$.ajax({
		url: 'http://gyp.meidongya.cn/download/get_code_rate',
		type: 'POST',
		dataType: 'json',
		success: function(data){
			var _html = '';
			for (var i = 0, length = data.length; i < length; i++) {
				_html += '<option value="'+data[i]['value']+'">'+data[i]['name']+'</option>';
			}
			$('#code_rate').html(_html);
			console.log(_html);
		}
	})
	.done(function() {
		console.log("success");
	})
	.fail(function() {
		console.log("error");
	})
	.always(function() {
		console.log("complete");
	});
	
	$.getUrlParam = function (name) {
                    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
                    var r = window.location.search.substr(1).match(reg);
                    if (r != null) return unescape(r[2]); return null;
                }
	var pid = $.getUrlParam('program_id');
	$('#manager').datagrid({
		url : 'http://gyp.meidongya.cn/program/get_seriers' ,
		fit : true,
		fitColumns : true,
		striped : true,
		rownumbers : true,
		border : false,
		nowrap:false,
		toolbar : '#manager_tool',
		queryParams: {
			program_id: pid
		},
		columns : [[
			{
				field : 'ck',
				width : 10,
				checkbox : true,
			},
			{
				field : 'id',
				title : '编号',
				width : 20,
			},
			{
				field : 'program_name',
				title : '剧集名',
				width : 120,
			},
			{
				field : 'episode',
				title : '序号',
				width : 20,
			},
			{
				field : 'status',
				title : '状态',
				width : 20,
				formatter: function(value,row,index){
					if(value==0){
						return '上线';
					} else{
						return '下线';
					}
				},
			},
			{
				field : 'upload_time',
				title : '上传时间',
				width : 50,
				formatter: function(value,row,index){
					var date = new Date();
					date.setTime(value * 1000);  
				    var y = date.getFullYear();      
				    var m = date.getMonth() + 1;      
				    m = m < 10 ? ('0' + m) : m;      
				    var d = date.getDate();      
				    d = d < 10 ? ('0' + d) : d;      
				    var h = date.getHours();    
				    h = h < 10 ? ('0' + h) : h;    
				    var minute = date.getMinutes();    
				    var second = date.getSeconds();    
				    minute = minute < 10 ? ('0' + minute) : minute;      
				    second = second < 10 ? ('0' + second) : second;     
				    return y + '-' + m + '-' + d+' '+h+':'+minute+':'+second; 
				},
				sortable:true,

			},
			{
				field:'_operate',
				title : '操作',
				width:30,
				align:'center',
				formatter:formatOper
			}
		]]  
	});

	function formatOper(val,row,index){  
    	return '<a href="#" onclick="qiyong('+index+')">手动弃用节目</a>';  
	};

	qiyong = function(index){  
    	$('#manager').datagrid('selectRow',index);// 关键在这里  
	    var row = $('#manager').datagrid('getSelected');  
	    //console.log(row.id);
	    if (row){  
	        $.ajax({
				url: 'http://gyp.meidongya.cn/program/changeuse',
				type: 'POST',
				data : {
						program_id : row.id
					},
				success: function(data){
					$.messager.show({
									title : '提示',
									msg : data,
					});
				}
			})
    	}  
	};  	

	$.extend($.fn.validatebox.defaults.rules, {
	    selectValueRequired: {
	        validator: function(value,param){
	            return $(param[0]).find("option:contains('"+value+"')").val() != '';
	        },
	    message: '状态还未选择'
     	}
	});
		
	$('#manager_edit').dialog({
		width : 350,
		title : '修改管理',
		modal : true,
		closed : true,
		iconCls : 'icon-user-add',
		buttons : [{
			text : '提交',
			iconCls : 'icon-edit-new',
			handler : function () {
				if ($('#manager_edit').form('validate')) {
					$.ajax({
						url : 'http://gyp.meidongya.cn/program/edit',
						type : 'post',
						data : {
							id : $('input[name="id"]').val(),
							status : $('input[name="status_edit"]').val(),
						},
						beforeSend : function () {
							$.messager.progress({
								text : '正在修改中...',
							});
						},
						success : function (data, response, status) {
							$.messager.progress('close');
							
							if (data > 0) {
								$.messager.show({
									title : '提示',
									msg : '修改管理成功',
								});
								$('#manager_edit').dialog('close').form('reset');
								$('#manager').datagrid('reload');
							} else {
								$.messager.alert('修改失败！', '未知错误或没有任何修改，请重试！', 'warning');
							}
						}
					});
				}
			},
		},{
			text : '取消',
			iconCls : 'icon-redo',
			handler : function () {
				$('#manager_edit').dialog('close').form('reset');
			},
		}],
	});
	
	manager_tool = {
		download : function(){
			var rows = $('#manager').datagrid('getSelections');
			// console.log(rows.length);
			var a = [];
			for(var i = 0;i<rows.length;i++){
				a.push(rows[i].id);
			}
				/*window.open("http://gyp.meidongya.cn/download/download_movie/program_id/"+a);*/
			// console.log(a);
			var code_rate = $('#code_rate').val();
			$('#form1')[0].pid.value = a;
			$('#form1')[0].code_rate.value = code_rate;
			$('#form1')[0].action = "http://gyp.meidongya.cn/download/download_movie";
    		$('#form1')[0].submit();		
		},
	};	
});